package org.hong.monkey.scheduler

/**
 * A simple listener for application events.
 *
 * This listener expects to hear events from a single application only. If events
 * from multiple applications are seen, the behavior is unspecified.
 */
private[monkey] class ApplicationEventListener extends MonkeyListener {
  var appName: Option[String] = None
  var appId: Option[String] = None
  var appAttemptId: Option[String] = None
  var monkeyUser: Option[String] = None
  var startTime: Option[Long] = None
  var endTime: Option[Long] = None
  var viewAcls: Option[String] = None
  var adminAcls: Option[String] = None

  override def onApplicationStart(applicationStart: MonkeyListenerApplicationStart) {
    appName = Some(applicationStart.appName)
    appId = applicationStart.appId
    appAttemptId = applicationStart.appAttemptId
    startTime = Some(applicationStart.time)
    monkeyUser = Some(applicationStart.monkeyUser)
  }

  override def onApplicationEnd(applicationEnd: MonkeyListenerApplicationEnd) {
    endTime = Some(applicationEnd.time)
  }

  override def onEnvironmentUpdate(environmentUpdate: MonkeyListenerEnvironmentUpdate) {
    synchronized {
      val environmentDetails = environmentUpdate.environmentDetails
      val allProperties = environmentDetails("Monkey Properties").toMap
      viewAcls = allProperties.get("monkey.ui.view.acls")
      adminAcls = allProperties.get("monkey.admin.acls")
    }
  }
}
